package com.googlecode.cnasort.cc.rmi.common;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

/**
 *
 */
public class ServerRmiStarter<T extends Remote> extends RmiStarter<T>
{
    public ServerRmiStarter(
        final Class<T> clazzToAddToServerCodebase,
        final Integer  createServerRegistryPort
        )
    {
        super(clazzToAddToServerCodebase, createServerRegistryPort);
    }

    public ServerRmiStarter(
        final Class<T> clazzToAddToServerCodebase,
        final boolean  createServerRegistryOnDefautPort
        )
    {
        super(clazzToAddToServerCodebase, createServerRegistryOnDefautPort);
    }

    public ServerRmiStarter( final Class<T> clazzToAddToServerCodebase )
    {
        super( clazzToAddToServerCodebase );
    }

    public T exportObject( final T engine, final String serviceName ) throws RemoteException
    {
        @SuppressWarnings("unchecked")
        T engineStub = (T)UnicastRemoteObject.exportObject(engine, 0);

        Registry registry = LocateRegistry.getRegistry();
        registry.rebind( serviceName, engineStub );

        return engineStub;
    }
}
